﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DiscCalc
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            dispatcherTimer.Start();
        }
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText(TextDataFormat.Text))
            {
                String cb = Clipboard.GetText(TextDataFormat.Text);
                if (!cb.Contains("*** Cards Data Begins Here ***"))
                {
                    tb.Text = "Clipboard doesn't seem to contain game log.";
                    quenDmg.Text = "";
                    marcherDmg.Text = "";
                    return;
                }
                cb = cb.Substring(cb.IndexOf("*** Cards Data Begins Here ***"));
                int[] p1data = new int[3];
                int[] p2data = new int[3];
                parsePlayerData("P1",p1data, cb);
                parsePlayerData("P2", p2data, cb);
                tb.Text = "Pokemon: \n" + p1data[0] + " (P1)\n" + p2data[0] + " (P2)\n\nNight Marchers: \n" + p1data[1] + " (P1)\n" + p2data[1] + " (P2)\n\nEnergy: \n" + p1data[2] + " (P1)\n" + p2data[2] + " (P2)";

                int dmg1 = p1data[0] * 10 + 20;
                int dmg2 = p2data[0] * 10 + 20;
                int mdmg1 = p1data[1] * 20;
                int mdmg2 = p2data[1] * 20;
                if (dmg1 >= 170)
                {
                    quenDmg.Foreground = Brushes.Green;
                }
                if (mdmg1 >= 170)
                {
                    marcherDmg.Foreground = Brushes.Green;
                }
                quenDmg.Text = dmg1.ToString();
                marcherDmg.Text = mdmg1.ToString();
                if (dmg2 >= 170)
                {
                    quenDmg_2.Foreground = Brushes.Green;
                }
                if (mdmg2 >= 170)
                {
                    marcherDmg_2.Foreground = Brushes.Green;
                }
                quenDmg_2.Text = dmg2.ToString();
                marcherDmg_2.Text = mdmg2.ToString();

            }
        }

        public class Card
        {
            
            public string type;
            public string name;
            public string setname;
            public string setnumber;
            public Card(string ctype, string nm, string set, string num)
            {
                type = ctype;
                name = nm;
                setname = set;
                setnumber = num;
            }


        }

        private void parsePlayerData(String player, int[] data, String cb)
        {
            int reportedDiscards = 0;
            int countedDiscards = 0;
            string allDiscardsText;
            Regex r1 = new Regex(player + @" Cards In Discard \(Count: ([0-9]+)\)(.+)" + player + " Active Pokemon", RegexOptions.Singleline);
            Match m = r1.Match(cb);
            if (m.Success)
            {
                Int32.TryParse(m.Groups[1].Value, out reportedDiscards);
                allDiscardsText = m.Groups[2].Value;
            }
            else
            {
                return;
            }
            List<Card> cards = new List<Card>();
            string pattern = @"(?s)Card #(\d+) in card group:(.+?)Owner:";
            foreach (Match match in Regex.Matches(allDiscardsText, pattern))
            {
                Match cardMatch = Regex.Match(match.Groups[2].Value, @"(?s)Name: ([^\r\n]+).*Set Name: ([^\r\n]+).*Set Number: ([^\r\n]+).*Card Type: ([^\r\n]+).*");
                cards.Add(new Card(cardMatch.Groups[4].Value, cardMatch.Groups[1].Value, cardMatch.Groups[2].Value, cardMatch.Groups[3].Value));
                countedDiscards++;
            }
            if (reportedDiscards != countedDiscards)
            {
                tb.Text = "Problem: counted a different number of discards than the log indicated.";
                return;
            }
            int pokeCount = 0;
            int energyCount = 0;
            int nightMarchers = 0;
            foreach (Card card in cards)
            {
                if (card.type == "Pokemon")
                {
                    pokeCount++;
                    if ((card.name == "Lampent" || card.name == "Joltik" || card.name == "Pumpkaboo") && card.setname.Contains("Phantom Forces"))
                    {
                        nightMarchers++;
                    }
                }
                else if (card.type == "Energy")
                {
                    energyCount++;
                }
            }
            data[0] = pokeCount;
            data[1] = nightMarchers;
            data[2] = energyCount;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
